import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Excel {

//    @Test
    public void test()throws Exception{
        XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File("C:\\Users\\admin\\Desktop\\c.xlsx")));

        XSSFSheet sheet = excel.getSheetAt(0);

        for (Row row : sheet) {
            for (Cell cell : row) {
                System.out.println(cell.getStringCellValue());
            }
            System.out.println();
        }
        excel.close();
    }


//    @Test
    public void test2()throws Exception{

        XSSFWorkbook excel = new XSSFWorkbook();
        XSSFSheet sheet = excel.createSheet("haas");
        XSSFRow title = sheet.createRow(0);

        title.createCell(0).setCellValue("姓名");
        title.createCell(1).setCellValue("地址");
        title.createCell(2).setCellValue("年龄");

        XSSFRow dataRow = sheet.createRow(1);
        dataRow.createCell(0).setCellValue("小明");
        dataRow.createCell(1).setCellValue("北京");
        dataRow.createCell(2).setCellValue("20");

        //创建一个输出流，通过输出流将内存中的Excel文件写到磁盘
        FileOutputStream out = new FileOutputStream(new File("C:\\Users\\admin\\Desktop\\d.xlsx"));
        excel.write(out);
        out.flush();
        excel.close();

    }
}
